﻿using System;
using System.Linq;
using System.Web;
using CSS;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Utils = Thuoc24h.Common.Utils;
using System.Collections.Generic;
using Thuoc24h.Common.Enums;

namespace Thuoc24h.Home.Pages
{
    public partial class ProductDetail : BasePage.BasePages
    {
        public int productId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = Page.RouteData.Values["id"].ToInt32Return0();
            productId = id;
            if (!Page.IsPostBack)
            {
                var product = ReviewRepository.GetProductById(id);
                ltrName.Text = ProductNameLtr.Text = Page.Title = product.Name;
                double priceDes = (double)((product.Price / 100) * (100 - product.DiscountPercent));
                ltrPrice.Text = priceDes.FormatMoney() + "/" + (Utils.GetEnumObjectByValue<Unit>(product.Unit)).ToString().ToLower(); ;
                ltrPriceOld.Text = product.Price.FormatMoney("&nbsp;");
                if (product.IsSalesOff == true)
                {
                    ltrPromotion.Text = "<div id=\"ProductPromotion\"><h3>Khuyến mại:</h3><div>" + product.DiscountDescription + "</div></div>";
                }
                if (product.DiscountPercent > 0)
                {
                    ltrPriceDiscount.Text = "-" + product.DiscountPercent + "%";
                }
                ltrProductDesc.Text = product.Description;
                ltrDetail.Text = product.Detail;
                ltrBtnBuy.Text = "<a href=\"/" + Utils.BuildUrl("mua-hang", product.Id) + "\" >ĐẶT MUA</a>";

                string prostr = "", imagepagestr = "";
                int index = -1;
                foreach (var item in ReviewRepository.GetImageByProduct(id))
                {
                    if (index > 3)
                    {
                        break;
                    }
                    index++;
                    prostr += "<li><img src=\"" + item.ImageUrl + "\" /></li>";
                    imagepagestr += "<a data-slide-index=\"" + index + "\" href=\"\"><img src=\"" + item.ImageUrl + "\" /></a>";
                }
                ltrProductImageThum.Text = prostr;
                ltrImagePager.Text = imagepagestr;
                UpdateViewCount(id);
                var unit = product.Unit;
                var priceUnits = context.PriceUnits.Where(it => it.Unit != unit && it.ProductId == product.Id);
                if (priceUnits.Count() > 0)
                {
                    List<UnitPrice> unitP = new List<UnitPrice>();
                    foreach (var priceUnit in priceUnits)
                    {
                        if (priceUnit.Price > 0)
                        {
                            var p = priceUnit.Price.FormatMoney();
                            var uId = byte.Parse(priceUnit.Unit.ToString());
                            var u = (Utils.GetEnumObjectByValue<Unit>(uId)).ToString().ToLower();
                            var disCount = priceUnit.DiscountPercent;
                            var d = "---";
                            if (disCount > 0)
                            {
                                d = (priceUnit.Price - priceUnit.Price * disCount).FormatMoney();
                            }
                            unitP.Add(new UnitPrice
                                           {
                                               Price = p,
                                               Unit = u,
                                               DiscountPrice = d
                                           });
                        }
                    }
                    UnitPricesRpt.DataSource = unitP;
                    UnitPricesRpt.DataBind();
                    if (unitP.Count() == 0)
                    {
                        UnitPricesRpt.Visible = false;
                    }
                }
            }
        }
        public class UnitPrice
        {
            public string Price { get; set; }
            public string DiscountPrice { get; set; }
            public string Unit { get; set; }
        }

        private void UpdateViewCount(int id)
        {


            var view = "productViewCount" + id;
            var cookied = Request.Cookies[view];
            if (cookied == null)
            {
                var product = context.Products.SingleOrDefault(item => item.Id == id);

                if (product != null)
                {
                    product.ViewCount += 1;
                    context.SaveChanges();
                }

                var cookie = new HttpCookie(view) { Value = view, Expires = DateTime.Now + new TimeSpan(0, 0, 30, 0) };
                Response.Cookies.Add(cookie);
            }



        }
    }
}